package com.dao.impl;

import com.bean.Record;
import com.bean.User;
import com.dao.RecordDao;
import com.util.JDBC_Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RecordDaoImpl implements RecordDao {

    Connection conn = null;
    ResultSet rs= null;
    PreparedStatement ps = null;

    @Override
    public int addRecord(User user) {
        java.util.Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = simpleDateFormat.format(date) + "";

        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss");
        String Date1 = simpleDateFormat1.format(date) + "";

        String realName = user.getRealName();
        int id = user.getId();
        String str = realName+"在"+Date1+"登录了系统";
        String sql = "insert into t_record values (null,'"+nowDate+"',"+id+",'"+str+"')";

        int n = JDBC_Util.update(sql);

        return n;
    }

    @Override
    public List<Record> getAllRecord() {
        List<Record> list = new ArrayList<>();

        String sql = "select id, date, info from t_record";

        conn = JDBC_Util.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String date = rs.getString("date");
                String info = rs.getString("info");

                Record record = new Record();
                record.setDate(date);
                record.setId(id);
                record.setInfo(info);
                list.add(record);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }

    public List<Record> findRecords(String startDate,String endDate) {
        List<Record> list = new ArrayList<>();

        String sql = "select id, date, info from t_record where 1 = 1";

        if(!startDate.equals("")  && startDate != null){
            sql += " and date > '"+startDate+"'";
        }

        if(!endDate.equals("")  && endDate != null){
            sql += " and date < '"+endDate+"'";
        }

        conn = JDBC_Util.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String date = rs.getString("date");
                String info = rs.getString("info");

                Record record = new Record();
                record.setDate(date);
                record.setId(id);
                record.setInfo(info);
                list.add(record);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBC_Util.close(rs,conn,ps);
        }
        return list;
    }
}
